from collections import OrderedDict
from apps.goods.models import GoodsChannel


def get_categories():
    # 封装商品 分类函数'contents':contents

    # 获取37个频道
    channels = GoodsChannel.objects.order_by('group_id', 'sequence')
    # 前端要得是字典，设置容器
    categories = OrderedDict()
    # 让阿后找到对应的频道组表
    for channel in channels:
        group_id = channel.group_id
        if group_id not in categories:
            categories[group_id] = {'channels': [], 'sub_cats': []}
            # 再找到下面的分组
        cat1 = channel.category
        # 将一级分类的数据 啊炸前端的格式进行拼接
        # categories[group_id]['channels'].append({
        #     'id':cat1.id,
        #     'name':cat1.name,
        #     'url':channel.url,
        # })
        cat1.url = channel.url
        categories[group_id]['channels'].append(cat1)
        # 将数据一级分类链接二级然后链接三级
        for cat2 in cat1.subs.all():
            cat2.sub_cats = []
            for cat3 in cat2.subs.all():
                cat2.sub_cats.append(cat3)
            categories[group_id]['sub_cats'].append(cat2)
    return categories

